package org.middlewaregr.bp;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.jbpm.api.Configuration;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.RepositoryService;
import org.jbpm.api.TaskService;
import org.jbpm.api.task.Task;

public class Main {

	public static ProcessEngine processEngine;

	public static void main(String[] args) {

		// Build jBPM services
		processEngine = new Configuration().setResource("my.jbpm.cfg.xml")
				.buildProcessEngine();
		// ProcessEngine processEngine = new
		// Configuration().buildProcessEngine();
		RepositoryService repositoryService = processEngine
				.getRepositoryService();
		ExecutionService executionService = processEngine.getExecutionService();

		// Deploy helloWorld process definition and start a process instance
		repositoryService.createDeployment().addResourceFromClasspath(
				"theses_process.jpdl.xml").deploy();
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("teacher_id", "johndoe");
		variables.put("student_id", "george");
		variables.put("proposal_id", -1);
		variables.put("theses_id", -1);
		variables.put("title", "customTitle");
		variables.put("consultant", "customConsultant");
		variables.put("proposal_description", "proposal_description");
		variables.put("technologies", "jbpm and java :)");
		variables.put("execution_time", 5);

		executionService.startProcessInstanceByKey("ThesesProcess", variables);

		TaskService taskService = processEngine.getTaskService();
		Iterator<Task> iterator = taskService.findPersonalTasks("johndoe")
				.iterator();
		Task nextTask = (Task) iterator.next();
		String taskId = nextTask.getId();
		Set<String> variableNames = taskService.getVariableNames(taskId);
		variables = taskService.getVariables(taskId, variableNames);
		variables.put("approval_description", "approved bace");
		variables.put("reviewer", "pesho");

		taskService.completeTask(nextTask.getId(), "to ApproveProposalTask",
				variables);

		iterator = taskService.findPersonalTasks("george").iterator();
		nextTask = (Task) iterator.next();
		taskId = nextTask.getId();
		variableNames = taskService.getVariableNames(taskId);
		variables = taskService.getVariables(taskId, variableNames);
		variables.put("graduation_date", "2010-10-10");
		variables.put("file_location", "c:\neshto si");
		variables.put("department", "deferencialni uravneniq");
		variables.put("faculty", "BLA");

		taskService.completeTask(nextTask.getId(), variables);

		iterator = taskService.findPersonalTasks("pesho").iterator();
		nextTask = (Task) iterator.next();
		taskId = nextTask.getId();
		variableNames = taskService.getVariableNames(taskId);
		variables = taskService.getVariables(taskId, variableNames);
		variables.put("relevance_to_topic", "nishto obshto");
		variables.put("review_description", "ne q e praivil toi");
		variables.put("layout_and_style", "mnogo zle");
		variables.put("contributions", "nishto");
		variables.put("conclusion", "nishto");
		variables.put("contributions", "nishto");
		variables.put("recommendations", "nqma");
		variables.put("assessment", 2);

		taskService.completeTask(nextTask.getId(), variables);
	}
}
